package cn.xlsea.gen;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.CaseUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xlsea
 * @since 2023-07-02
 */
@Slf4j
@Service
public class AizudaWebGenService {

    @Resource
    private FactoryProperties project;

    public void main(){
        log.info("开始执行");
        this.fileGenerator();
        log.info("执行完成");
    }

    private void fileGenerator() {
        String srcPath = project.getFactoryPath() + "/aizuda-boot-web";
        if (StringUtils.isNotEmpty(srcPath)) {
            File srcFile = new File(srcPath);

            String destPath = project.getServerRootPath() + "/aizuda-boot-web";
            File destRoot = new File(destPath);
            List<String> exclude = this.getExcludeFile();

            try {
                FileUtils.copyDirectory(srcFile, destRoot, file -> !exclude.contains(file.getName()));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            try {
                this.fileContentFormat(destRoot);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private List<String> getExcludeFile () {
        List<String> exclude = new ArrayList<>();
        exclude.add(".git");
        exclude.add("node_modules");
        exclude.add(".DS_Store");
        exclude.add(".idea");
        exclude.add(".vscode");
        return exclude;
    }

    private void fileContentFormat (File destRoot) throws IOException {
        final String varNamePrefix = CaseUtils.toCamelCase(project.getProjectName(), false, '-', '_');

        FileFormatter jsFormatter = new FileFormatter(destRoot, FileFilterUtils.suffixFileFilter("js"));
        jsFormatter.replaceAll("AiZuDa", project.getProjectTitle());
        jsFormatter.replaceAll("Aizuda", project.getProjectName());
        jsFormatter.replaceAll("aizuda", varNamePrefix);
        jsFormatter.format();

        FileFormatter vueFormatter = new FileFormatter(destRoot, FileFilterUtils.suffixFileFilter("vue"));
        vueFormatter.replaceAll("AiZuDa", project.getProjectTitle());
        vueFormatter.replaceAll("Aizuda", project.getProjectName());
        vueFormatter.replaceAll("aizuda", varNamePrefix);
        vueFormatter.format();

        FileFormatter jsonFormatter = new FileFormatter(destRoot, FileFilterUtils.suffixFileFilter("json"));
        jsonFormatter.replaceAll("AiZuDa", project.getProjectTitle());
        jsonFormatter.replaceAll("Aizuda", project.getProjectName());
        jsonFormatter.replaceAll("aizuda", varNamePrefix);
        jsonFormatter.format();

        FileFormatter developmentFormatter = new FileFormatter(destRoot, FileFilterUtils.suffixFileFilter("development"));
        developmentFormatter.replaceAll("AiZuDa", project.getProjectTitle());
        developmentFormatter.replaceAll("Aizuda", project.getProjectName());
        developmentFormatter.replaceAll("aizuda", varNamePrefix);
        developmentFormatter.format();

        FileFormatter productionFormatter = new FileFormatter(destRoot, FileFilterUtils.suffixFileFilter("production"));
        productionFormatter.replaceAll("AiZuDa", project.getProjectTitle());
        productionFormatter.replaceAll("Aizuda", project.getProjectName());
        productionFormatter.replaceAll("aizuda", varNamePrefix);
        productionFormatter.format();
    }

}
